package managedbean;

import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import session.stateless.EmployeeSessionBeanLocal;
import entity.Message;
import java.util.Arrays;
import java.util.TimeZone;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.model.MenuModel;

@ManagedBean(name = "defaultManagerBean")
@RequestScoped
public class DefaultManagerBean {

    @EJB
    EmployeeSessionBeanLocal employeeSessionBeanLocal;
    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    private String loadDefault;
    private String loadIndex;
    private MenuModel model;
    private String getCurrentPage;
    private String getCurrentPageFormatted;
    private Boolean checkPageAccessRight = false;
    private TimeZone tz;

    public DefaultManagerBean() {
    }

    public String getLoadDefault() throws IOException {
        if (userSessionManagerBean.getUserId() == null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" 
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort() + "/MerlionERP-war/login.xhtml");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            setTz(TimeZone.getDefault());
            
            getCurrentPage = FacesContext.getCurrentInstance().getViewRoot().getViewId();

            StringBuffer buf = new StringBuffer(getCurrentPage.length() - 1);
            buf.append(getCurrentPage.substring(0, 0)).append(getCurrentPage.substring(0 + 1));
            
            getCurrentPageFormatted = buf.toString();

            if (userSessionManagerBean.getOTP() && getCurrentPageFormatted.compareTo("changePassword.xhtml") != 0) {
                try {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                            + userSessionManagerBean.getServerPort() + "/MerlionERP-war/changePassword.xhtml");

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            checkPageAccessRight = false;
            for (String a : userSessionManagerBean.getAddressList()) {
                if (a.compareTo(getCurrentPageFormatted) == 0) {
                    checkPageAccessRight = true;
                    
                    break;
                }
            }

            model = userSessionManagerBean.getModel();
        }
        
        if (checkPageAccessRight == false) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/forbidden.xhtml");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return null;
    }

    public void setLoadDefault(String loadDefault) {
        this.loadDefault = loadDefault;
    }
    // <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">

    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    public MenuModel getModel() {
        return model;
    }

    public void setModel(MenuModel model) {
        this.model = model;
    }

    public TimeZone getTz() {
        return tz;
    }

    public void setTz(TimeZone tz) {
        this.tz = tz;
    }
// </editor-fold>
}
